// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Test that we can load a wasm module, find a function, and call it.
import 'dart:typed_data';

import 'package:test/test.dart';
import 'package:wasm/wasm.dart';

import 'test_shared.dart';

void main() {
  test('basics', () {
    // (func $foo (export "foo") (type $t0) (unreachable))
    final data = Uint8List.fromList([
      0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, //
      0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, 0x01, 0x07,
      0x07, 0x05, 0x03, 0x01, 0x00, 0x11, 0x07, 0x10, 0x02, 0x03, 0x66, 0x6f,
      0x6f, 0x00, 0x00, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00,
      0x0a, 0x05, 0x01, 0x03, 0x00, 0x00, 0x0b, 0x00, 0x2b, 0x04, 0x6e, 0x61,
      0x6d, 0x65, 0x01, 0x06, 0x01, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x03,
      0x01, 0x00, 0x00, 0x04, 0x05, 0x01, 0x00, 0x02, 0x74, 0x30, 0x05, 0x05,
      0x01, 0x00, 0x02, 0x54, 0x30, 0x06, 0x09, 0x01, 0x00, 0x06, 0x6d, 0x65,
      0x6d, 0x6f, 0x72, 0x79,
    ]);

    final inst = wasmModuleCompileSync(data).builder().build();
    final foo = inst.lookupFunction('foo');
    // ignore: unnecessary_lambdas
    expect(() => foo(), throwsWasmException('unreachable'));
  });
}
